package com.ja.openchat.ui.contact.search;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.ja.openchat.BR;
import com.ja.openchat.R;
import com.ja.openchat.databinding.FragmentChatSearchPageBinding;
import com.ja.openchat.ui.chat.ChatActivity;
import com.ja.openchat.ui.contact.ContactDetailActivity;
import com.ja.openchat.ui.contact.ContactDetailSingleActivity;
import com.ja.openchat.viewmodels.ChatSearchPageViewModel;
import com.ja.openchat.viewmodels.ChatSearchViewModel;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.model.message.ChatInfo;
import com.zy.openchat.core.model.message.MessageInfo;
import com.zy.openchat.core.model.user.UserInfo;

import me.goldze.mvvmhabit.base.BaseFragment;

//
public class ChatSearchPageFragment extends BaseFragment<FragmentChatSearchPageBinding, ChatSearchPageViewModel> {

    private int type;
    private PublicSearchSectionAdapter publicSearchSectionAdapter;

    public static ChatSearchPageFragment newInstance(@IntRange(from = 0, to = 2) int type, long userId) {
        ChatSearchPageFragment fragment = new ChatSearchPageFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        bundle.putLong("userId", userId);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_chat_search_page;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }


    @Override
    public void initView() {
//        binding.recyclerView.
        initList();
    }

    private void initList() {
        //初始化列表
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.recyclerView.setLayoutManager(layoutManager);
        publicSearchSectionAdapter = new PublicSearchSectionAdapter(R.layout.adapter_section_contact_list, viewModel.sectionList);
        binding.recyclerView.setAdapter(publicSearchSectionAdapter);
        viewModel.setAdapter(publicSearchSectionAdapter);
        //点击事件
        publicSearchSectionAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (viewModel.sectionList != null && viewModel.sectionList.size() > 0 && position >= 0 && position < viewModel.sectionList.size()) {
                    Object obj = viewModel.sectionList.get(position).getObject();
                    if (obj instanceof UserInfo) {
                        UserInfo user = (UserInfo) obj;
                        //跳转个人资料详情
//                        ContactDetailActivity.start(getActivity(), user);
                        ContactDetailSingleActivity.start(getActivity(), user);
                    }else if(obj instanceof MessageInfo){
                        MessageInfo messageInfo = (MessageInfo)obj;
                        ChatInfo chat = TelegramManager.getTelegramManager().getTgData().chatInfo(messageInfo.chat_id);
                        if(chat!=null){
                            ChatActivity.startActivity(getActivity(),chat);
                        }
                    }else if(obj instanceof ChatInfo){
                        ChatInfo chat = (ChatInfo) obj;
                        if(chat!=null){
                            ChatActivity.startActivity(getActivity(),chat);
                        }
                    }
                }
            }
        });
    }

    @Override
    public void initData() {
        Bundle arguments = getArguments();
        if (arguments != null) {
            type = arguments.getInt("type");
//            viewModel.setType(type);
//            viewModel.setData(arguments);
        }

        ChatSearchViewModel chatSearchViewModel = ViewModelProviders.of(getActivity()).get(ChatSearchViewModel.class);
        viewModel.setChatSearchViewModel(chatSearchViewModel);
        viewModel.setActivity(getActivity());
    }

    public void doSearch(int position) {
        viewModel.doSearch(position);
    }

    @Override
    public void initViewObservable() {
        viewModel.setPosition(type);
//        //监听下拉刷新完成
//        viewModel.uc.finishRefreshing.observe(this, o -> {
//            //结束刷新
//            binding.refreshLayout.finishRefresh();
//        });
//        //监听上拉加载完成
//        viewModel.uc.finishLoadmore.observe(this, o -> {
//            //结束刷新
//            binding.refreshLayout.finishLoadMore();
//        });
//        //监听是否启用加载更多
//        viewModel.uc.enableFinishLoadmore.observe(this, aBoolean -> {
//            KLog.d("enableFinishLoadmore==" + aBoolean);
//            binding.refreshLayout.setEnableLoadMore(aBoolean);
//        });
    }
}
